MyDesktop.EmployeeWindow = Ext.extend(Ext.app.Module, {
    id:'employee-win',
    init : function(){
        this.launcher = {
            text: '员工管理',
            iconCls:'accordion',
            handler : this.createWindow,
            scope: this
        }
    },
	
	record : Ext.data.Record.create([
	    {name : 'id',mapping : 'id',type : 'int'}, 
	    {name : 'name',mapping : 'name',type : 'string'}, 
		{name : 'namePinyin',mapping : 'namePinyin',type : 'string'},
	    {name : 'idcard',mapping : 'idcard',type : 'string'}, 
	    {name : 'intime',mapping : 'intime',type : 'string'}, 
		{name : 'inJob',mapping : 'inJob',type : 'string'}, 
		{name : 'sex',mapping : 'sex',type : 'string'}, 
	    {name : 'status',mapping : 'status',type : 'int'}
	   ]),
	cm : new Ext.grid.ColumnModel([
	       new Ext.grid.RowNumberer(),
		   {header : '姓名',width : 40,dataIndex : 'name'}, 
		   {header : '拼音',width : 40,dataIndex : 'namePinyin'},
		   {header : '身份证',width : 60,dataIndex : 'idcard'},
		   {header: "性别", width: 20, dataIndex: 'sex'},
		   {header: "入职时间", width: 20, dataIndex: 'intime'}
	]),
	
	getPanel:function(){
		 	 var store=new Ext.data.GroupingStore({
	       		url : app.url+'/admin/employeeList.do',
				baseParams:{start:0,limit:20},
				autoLoad : true,
	       	 	reader : new Ext.data.JsonReader({totalProperty : 'totalProperty',root : 'root'}, this.record),
				groupField:'sex'
       		 });
       		 
       		 var employee_detailForm = new Ext.FormPanel({
       		 	 labelSeparator : "：",
				 frame:true,
				 border:false,
				 defaultType : 'textfield',
				 defaults:{width:160},
				 items:[
				     {
				      	fieldLabel:'姓名',
				      	name:"name"
				     },{
				     	xtype:'numberfield',
				     	fieldLabel:'身份证号',
				      	name:"idcard",
				      	maxLength : 18,
					  	minLength : 15
				     },{
				      	fieldLabel:'年龄',
				     	name:"age"
				     },{
				    	xtype : 'radiogroup',
						fieldLabel : '性别',
						name : 'sex',
						items:[
						   {
						     boxLabel : '男',
							 name : 'sex',
							 inputValue : '1'
						   }, {
							 boxLabel : '女',
							 name : 'sex',
							 inputValue : '0'
						   }
						]
				    },{
				    	fieldLabel:'联系电话',
				    	name:'phone'
				    },{
				       fieldLabel:'籍贯',
				       name:"areaId"
				    },{
				       fieldLabel:'家庭住址',
				       name:'address'
				    },{
				       fieldLabel:'紧急情况联系人',
				       name:'emergencyMan'
				    },{
				       fieldLabel:'联系人电话',
				       name:'emergencyCall'
				    },{
				       fieldLabel:'工号',
				       name:'empID'
				    },{
				       fieldLabel:'提成',
				       name:'percentage'
				    },{
				       fieldLabel:'职位',
				       name:'position'
				    },{ 
				      xtype:'textarea',
				      fieldLabel:'备注',
				      name:'memo'
				    }],
				 buttons:[
			       {
			     	id:'commitButton',
			        handler:submitForm
			      },{
			     	id:'resetButton',
			        handler:resetForm
			      },{
			     	 id:'closeButton',
			     	 handler:function(){				     		
			     	    addOrEditEmployee_win.hide();
			     	}
			     }
			]
       		 });
       		 
       		 var addOrEditEmployee_win = new Ext.Window({
       		 	layout:'fit',
		    	width:380,
		    	closeAction:'hide',
		   		height:500,
				resizable :false,
				shadow : true,
				modal :true,
		   	    closable:true,
		    	bodyStyle:'padding:5 5 5 5',		    	
		    	animCollapse:true,
				items:[employee_detailForm]
       		 });
       		 
       		 var employee_gridPanel =  new Ext.grid.GridPanel({
					    title : '在职员工',
                        border:false,
						columnLines :true,
                        enableHdMenu : false,
                        ds: store,
                        cm: this.cm,
						view: new Ext.grid.GroupingView({
                            forceFit:true,
                            groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'
                           }),
                        viewConfig: {
                            forceFit:true
                        },
                        tbar:[{
                            text:'增加',
                            tooltip:'增加员工信息',
                            iconCls:'user-add',
                            handler:addEmployee
                        }, '-', {
                            text:'修改',
                            tooltip:'修改员工信息',
                            iconCls:'option',
                            handler:editEmployee
                        },'-',{
                            text:'删除',
                            tooltip:'删除员工信息',
                            iconCls:'user-delete',
                            handler:deleteEmployee
                        }],
						bbar: new Ext.PagingToolbar({
                            pageSize: 20,
                            store: store,
                            displayInfo: true,
                            plugins: new Ext.ux.ProgressBarPager()
                        })
            });
            
            var joinPanel = [
            	employee_gridPanel,
            	{
                   title: '离职员工',
                   html:'<p>Something useful would be in here.</p>',
                   autoScroll:true
                  }
            ]
            return joinPanel;
            
              //取得选中的记录
	 		function getSelectedItem(){
	     		var record= employee_gridPanel.getSelectionModel().getSelected();	   
	     		if(record == null){
	        		 Ext.MessageBox.alert('提示','请选择要修改的明细！');
	         		return;
	     		}else{
	        		return record;
	     		}
	  		}
            
            function addEmployee(){
            	if(employee_detailForm.form.el){
            		employee_detailForm.form.el.dom.reset();            	
            	}else{
            		employee_detailForm.form.reset();
            	}
                Ext.getCmp('commitButton').setText('新增');
		    	Ext.getCmp('resetButton').setText('重置');
		        Ext.getCmp('closeButton').setText('关闭');
		        addOrEditEmployee_win.isAdd = true;
		        addOrEditEmployee_win.setTitle("新增员工");
		        addOrEditEmployee_win.show();
            }
            
            function editEmployee(){
            	if(employee_detailForm.form.el){
            		employee_detailForm.form.el.dom.reset();            	
            	}else{
            		employee_detailForm.form.reset();
            	}
            	var record=getSelectedItem();
            	if(record){
            		Ext.getCmp('commitButton').setText('修改');
		    		Ext.getCmp('resetButton').setText('重置');
		    		Ext.getCmp('closeButton').setText('关闭');
		    		addOrEditEmployee_win.setTitle("编辑入库明细");
		    		addOrEditEmployee_win.isAdd = false;
		    		employee_detailForm.getForm().loadRecord(record);
		    		addOrEditEmployee_win.show();
            	}else{
            		 Ext.MessageBox.alert('提示','请选择要修改的明细！');
            	}              	
            }
            
            function deleteEmployee(){
            }
            
            function resetForm(){
            }
            
            function submitForm(){
            	if(!employee_detailForm.getForm().isValid()){
	         		 return;
	      		}
	      		if(addOrEditEmployee_win.isAdd){
	      		   	Ext.Ajax.request({
	           			url:app.url+'/admin/createEmployee.do',
	          	 		//params :{},
	           			method :"post",
	           			callback :function(options,success,response){
	                     
	           			},
	          			success:function(response,options){
							Ext.MessageBox.alert("提示","保存成功!")
	           			},
	           			failure:function(response,options){
	                		Ext.MessageBox.alert("提示","发生异常，请稍候处理!");    
	           			}
	       			});
	      		}else{
            		Ext.Ajax.request({
	           			url:app.url+'/admin/updateEmployee.do',
	          	 		//params :{queryParam:ordersId},
	           			method :"post",
	           			callback :function(options,success,response){
	                     
	           			},
	          			success:function(response,options){
							Ext.MessageBox.alert("提示","修改成功!")
	           			},
	           			failure:function(response,options){
	                		Ext.MessageBox.alert("提示","发生异常，请稍候处理!");    
	           			}
	       			});
           		}
         	}
      },

    createWindow : function(){
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('employee-win');
		
		   
        if(!win){
        	
       		 
            win = desktop.createWindow({
                id: 'acc-win',
                title: '员工管理',
                width:850,
                height:500,
                iconCls: 'accordion',
                shim:false,
                animCollapse:false,
                constrainHeader:true,
                layout:'accordion',
                border:false,
                layoutConfig: {
                    animate:false
                },

                items: this.getPanel()
            });
            
           
        }
        win.show();
    }
});
